// ColorLightSensor.java

/*
This software is part of the NxtJLib library.
It is Open Source Free Software, so you may
- run the code for any purpose
- study how the code works and adapt it to your needs
- integrate all or parts of the code in your own programs
- redistribute copies of the code
- improve the code and release your improvements to the public
However the use of the code is entirely your responsibility.
*/

package gin.and.tonic;

import java.util.*;

/**
 * Class that represents a color light sensor (Lego Color Sensor)
 */
public class ColorLightSensor extends Sensor implements SharedConstants
{
  private lejos.nxt.ColorLightSensor cls;
  private lejos.nxt.SensorPort sp;

  /**
   * Creates a sensor instance connected to the given port.
   * Uses the lejos.nxt.ColorLightSensor in type TYPE_COLORFULL
   * @param port the port where the sensor is plugged-in
   */
  public ColorLightSensor(SensorPort port)
  {
    super(port);
    switch (port.getId())
    {
      case 0:
        sp = lejos.nxt.SensorPort.S1;
        break;
      case 1:
        sp = lejos.nxt.SensorPort.S2;
        break;
      case 2:
        sp = lejos.nxt.SensorPort.S3;
        break;
      case 3:
        sp = lejos.nxt.SensorPort.S4;
        break;
      default:
        sp = lejos.nxt.SensorPort.S1;
        break;
    }
    cls = new
      lejos.nxt.ColorLightSensor(sp, lejos.nxt.ColorLightSensor.TYPE_COLORFULL);
  }

  /**
   * Creates a sensor instance connected to port S1.
   * Uses the lejos.nxt.ColorLightSensor in type TYPE_COLORFULL
   */
  public ColorLightSensor()
  {
    this(SensorPort.S1);
  }

  protected void init()
  {
    if (NxtRobot.getDebugLevel() >= DEBUG_LEVEL_MEDIUM)
      DebugConsole.show("cls.init()");
  }

  protected void cleanup()
  {
    if (NxtRobot.getDebugLevel() >= DEBUG_LEVEL_MEDIUM)
      DebugConsole.show("cls.cleanup()");
  }

  /**
   * Returns the reference of the the underlying lejos.nxt.ColorLightSensor.
   * @return the reference of the LightSensor
   */
  public lejos.nxt.ColorLightSensor getLejosSensor()
  {
    return cls;
  }

  /**
   * Returns the reference of the the underlying lejos.nxt.SensorPort.
   * @return the reference of the SensorPort
   */
  public lejos.nxt.SensorPort getLejosPort()
  {
    return sp;
  }

  private int[] getLevel()
  {
    int[] init = {-1, -1, -1};
    if (robot == null)
      return init;
    return cls.getColor();
  }

  /**
   * Returns the RGB color values.<br>
   * Index 0: red component<br>
   * Index 1: green component<br>
   * Index 2: blue component<br>
   * The minimal poll period is about 500 ms.
   * @return the reference of an integer array of size 3
   */
  public int[] getRGB()
  {
    checkConnect();
    return cls.getColor();
  }

  /**
   * Checks if given rgb values lies within given color cube.
   * @param rgb an integer array of 3 color value
   * @param colorCube an integer array of 6 boundary values in the order:
   * red_min, red_max, green_min, green_max, blue_min, blue_max
   * @return true, if the rgb values are within the color cube; otherwise false
   */
  public boolean inColorCube(int[] rgb, int[] colorCube)
  {
    boolean inDomain = true;
    for (int k = 0; k < 3; k++)
    {
      if (rgb[k] < colorCube[2*k] || rgb[k] > colorCube[2*k + 1])
      {
        inDomain = false;
        break;
      }
    }
    return inDomain;
  }

  /**
   * Returns the Lego color value:<br>
   * 1: black<br>
   * 2: blue<br>
   * 3: green<br>
   * 4: yellow<br>
   * 5: red<br>
   * 6: white<br>
   * @return Lego color value
   */
  public int getColor()
  {
    checkConnect();
    return cls.readValue();
  }

  /**
   * Returns the intensity of the detected light.
   * @return light intensity (0..1023)
   */
  public int getLightValue()
  {
    checkConnect();
    return cls.getNormalizedLightValue();
  }

  /**
   * Turns the red, green or blue LED on or off.
   * (import of lejos.robotics.Color.Color needed)
   * @param color Color.RED, Color.GREEN, Color.BLUE, Color.NONE (turn off)
   */
  public void setFloodlight(lejos.robotics.Colors.Color color)
  {
    checkConnect();
    cls.setFloodlight(color);
  }

  private void checkConnect()
  {
    if (robot == null)
      new ShowError("ColorLightSensor (port: " + getPortLabel() +
        ") is not a part of the NxtRobot.\n" +
        "Call addPart() to assemble it.");
  }
}